package com.martini.satoken01.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.martini.satoken01.constant.ResCodeConstant;
import com.martini.satoken01.model.AjaxJson;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author martini at 2021/12/16 16:11
 */
@Slf4j
@RestController
@RequestMapping("/login")
public class LoginController {
    @PostMapping
    public AjaxJson handleLogin(@RequestBody Map<String, String> params) {
        if (StpUtil.isLogin()) {
            return AjaxJson.fail().setCode(ResCodeConstant.ALREADY_LOGIN).setMsg("已经登录");
        }
        if ("admin".equals(params.get("username")) && "admin".equals(params.get("password"))) {
            StpUtil.login(10001L);
            return AjaxJson.success().setData(StpUtil.getTokenInfo());
        }
        return AjaxJson.fail();
    }

    @GetMapping
    public AjaxJson isLogin() {
        return AjaxJson.success().setMsg("Session login: " + StpUtil.isLogin());
    }
}
